package com.scrawler.mysql.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.scrawler.util.DateUtils;
import com.scrawler.util.MysqlConfig;
import com.scrawler.vo.Keyword;

public class KeywordSql {

	
	 public static List<Keyword> getKeywordConfig(){
		 Connection conn = MysqlConfig.getConnection();
		 String sql = "select * from keyword";
		 List<Keyword> configs = new ArrayList<Keyword>();
		 Statement st = null;
		try {
			st = conn.createStatement();
			st.execute(sql);
			ResultSet rs = st.getResultSet();
			while(rs.next()){
				Keyword keyword = new Keyword();
				keyword.setId(rs.getInt("id"));
				keyword.setKeyword(rs.getString("keyword"));
				Timestamp stamp = rs.getTimestamp("start_time");
				
				System.out.println(stamp.toString());
				
				keyword.setStart_time(rs.getDate("start_time"));
				keyword.setLast_crawl_time(rs.getDate("last_crawl_time"));
				configs.add(keyword);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			MysqlConfig.close(conn, st);
		}
		 return configs;
	 }
	 
	 public static void updateLastCrawlTime(int id){
		 Connection conn = MysqlConfig.getConnection();
		 String sql = "update keyword set last_crawl_time='"+DateUtils.long2String(System.currentTimeMillis())+"' where id="+id;
		 Statement st = null;
		try {
			st = conn.createStatement();
			st.execute(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			MysqlConfig.close(conn, st);
		}
	 }
	 
	 public static void main(String args[]){
		 KeywordSql sql = new KeywordSql();
		 sql.getKeywordConfig();
		 sql.updateLastCrawlTime(1);
	 }
	
}
